package com.bzframework.bookkeeping.controller;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.bzframework.core.response.exception.BusinessException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author bingzhong
 * @Date 2025/11/2
 */
@Tag(name = "微信登录")
@RestController
@RequestMapping("/wx")
@RequiredArgsConstructor
public class WxLoginController {

    private final WxMaService wxMaService;

    @GetMapping("/login")
    @Operation(summary = "微信登录")
    public WxMaJscode2SessionResult wxLogin(@RequestParam String code) {
        try {
            WxMaJscode2SessionResult sessionInfo = wxMaService.getUserService().getSessionInfo(code);
            String openid = sessionInfo.getOpenid();
//        wxMaService.getUserService().getUserInfo()
            return sessionInfo;
        } catch (WxErrorException e) {
            throw new BusinessException(e.getMessage());
        }

    }

}
